package com.yibo.user.controller;

import com.yibo.support.pojo.IResult;
import com.yibo.support.pojo.Result;
import com.yibo.user.object.RegisterUser;
import com.yibo.user.object.YBUser;
import com.yibo.user.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import static com.yibo.support.constant.ApiUrlConstant.PUBLIC_URL;

@RestController
@RequestMapping(PUBLIC_URL+"/user")
public class PublicUserController {

    @Resource
    private UserService userService;


    @ApiOperation("通过用户名密码登录用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, type = "string"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, type = "string")
    })
    @PostMapping("/login/password")
    public IResult<YBUser>  loginByPassword(@RequestParam(value = "userId")String userId,
                                            @RequestParam(value = "password")String password,
                                            HttpServletResponse response){
        YBUser  user = userService.loginByPassword(userId,password);
        response.setHeader("token",user.getToken());
        return result("登录成功",user);
    }

    @ApiOperation("通过邮箱验证码登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "邮箱", required = true, type = "string"),
            @ApiImplicitParam(name = "checkWord", value = "验证码", required = true, type = "string")
    })
    @PostMapping("/login/email/checkword")
    public IResult<YBUser>  loginByEmailCheckWord(@RequestParam(value = "email")String email,
                                            @RequestParam(value = "checkWord")String checkWord,
                                            HttpServletResponse response){
        YBUser  user = userService.loginByEmail(email,checkWord);
        response.setHeader("token",user.getToken());
        return result("登录成功",user);
    }



    @ApiOperation("注册新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户ID",required = true,type = "string"),
            @ApiImplicitParam(name = "userName",value = "用户昵称",required = true,type = "string"),
            @ApiImplicitParam(name = "password",value = "密码",required = true,type = "string"),
            @ApiImplicitParam(name = "email",value = "邮箱",required = true,type = "string"),
            @ApiImplicitParam(name = "phone",value = "电话",required = true,type = "string"),
            @ApiImplicitParam(name = "checkEmailWord",value = "邮件验证码",required = true,type = "string")
     })
    @PostMapping("/register")
    public IResult<Integer>  register(@RequestBody RegisterUser registerUser){
        return result("注册成功",userService.register(registerUser));
    }



    private <T>IResult<T>  result(String msg,T  data){
        return new Result<>(msg,data);
    }
}
















